<?php
namespace Bus\Controller;
use Think\Controller;
use Common\Common\Wechat;
use common\Common\WechatAuth;

/**
 *
 */
class IndexController extends Controller
{

  public function index()
  {
    $appid = 'wxfea429ed2a074335'; //AppID(应用ID)
    $token = 'kathLeBlEcabJ4Lm48bAtbPMPZ4td8HL'; //微信后台填写的TOKEN
    //$crypt = 'q6FPCUoCQWaOiR3UUe5RfQu8A7hlJcMW4BnNyH9z2il'; //消息加密KEY（EncodingAESKey）

    $wechat=new Wechat($token);
    $data=$wechat->request();
    if($data && is_array($data))
    {
      
      $content = '回复了'; //回复内容，回复不同类型消息，内容的格式有所不同
    $type    = 'Wechat::MSG_TYPE_TEXT'; //回复消息的类型
    /* 响应当前请求(自动回复) */
    $wechat->response($content, $type);
    }
    $this->display();
  }

  //回复文本消息
  private function Text($wechat,$data)
  {
    if(strstr($data['Content'],"文本")){
      $text="文本消息回复!";

      $this->logger("发送消息：\n".$text);
      $wechat->replyText($text);
    }
    else
    {
      $text="文本消息没有回复!";

      $this->logger("发送消息：\n".$text);
      $wechat->replyText($text);
    }
  }

  private function logger($content)
  {
    $logSize=100000;
    $log="log.txt";
    if(file_exists($log) && filesize($log)>$logSize){
      unlink($log);
    }
    file_put_contents($log,data('H:i:s')." ".$content."\n",FILE_APPEND);
  }

}


?>
